<?php

namespace app\model;

use think\Model;

class OrderDealChargeback extends Model
{

    /**
     * 退单
     */
    protected $createTime='create_date';

    public function getGetUidUpLogoffFileAttr($value)
    {
        if (empty($value)) return "";
        $arr = explode(',',$value);
        $value = '';
        foreach($arr as &$v){
            $value .= config('file.url').$v .',';
        }
        return rtrim($value,',');
    }

    //B证电子证书文件
    public function getPushUidupLogoffFileAttr($value)
    {
        if (empty($value)) return "";
        $arr = explode(',',$value);
        $value = '';
        foreach($arr as &$v){
            $value .= config('file.url').$v .',';
        }
        return rtrim($value,',');
    }

    public function getBCertificateIsConpanyAttr($name)
    {
        if (!$name){
            return "";
        }
        return $name;
    }

    public function getCreateDateAttr($name)
    {
//        return date ('Y-m-d',strtotime ($name));
        return $name;
    }

    public function addDataModel($data)
    {
        return self::create ($data);
    }
    /**
     * 添加第二步资料
     * @param $cer
     * @param $param
     * @return \app\model\OrderDealChargeback
     */
    public function editCerDataModel($cer,$param)
    {
        //修改创建时间
        $param['create_date'] = date("Y-m-d H:i:s");
        return $this->where (['certificate_code'=>$cer])->update ($param);
    }

    /**
     * 获取第一步资料
     * @param $uid
     * @param $certificateCode
     * @return array|null
     */
    public function getNotifierGetUidModel($uid,$certificateCode,$id): ?array
    {
//        $data = $this->where (['notifier_get_uid'=>$uid,'certificate_code'=>$certificateCode,'order_company_id'=>$id])->findOrEmpty ();
        $data = $this->where (['certificate_code'=>$certificateCode,'order_company_id'=>$id])->whereNull('delete_date')->order('create_date DESC')->findOrEmpty ();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return [];
    }

    /**
     * 查询是否有退单或者续签操作
     * @param $cid
     * @param $cer
     * @return array|string
     */
    public function getCidCerDataModel($cid,$cer)
    {

        $find = $this->where (['certificate_code'=>$cer,'order_company_id'=>$cid])->whereNull('delete_date')->findOrEmpty ();

        if (!$find->isEmpty ()){
            return $find->toArray ();
        }
        return '';
    }

    /**
     * 查询该订单指定日期是否有退单
     * @param $cid
     * @param $cer
     * @param $start
     * @param $end
     * @return array|string
     */
    public function getCidCerDateFindModel($cid,$cer,$start,$end)
    {

        $find = $this->where (['certificate_code'=>$cer,'order_company_id'=>$cid])
            ->whereBetweenTime ('create_date',$start,$end)
            ->whereNull('delete_date')
            ->findOrEmpty ();

        if (!$find->isEmpty ()){
            return $find->toArray ();
        }
        return '';
    }

    /**
     * 查询退单操作
     * @param $cid
     * @param $cer
     * @param $startDate
     * @param $endDate
     * @return array|string
     */
    public function getCidCerTypeDataModel($cid,$cer,$startDate,$endDate)
    {

        $find = $this->where (['certificate_code'=>$cer,'order_company_id'=>$cid,'type'=>1])->whereBetweenTime ('create_date',$startDate,$endDate)->whereNull('delete_date')->findOrEmpty ();

        if (!$find->isEmpty ()){
            return $find->toArray ();
        }
        return '';
    }

    /**
     * 查询时间段内退单
     * @param $start
     * @param $end
     * @param $type
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getBetDateTypeModel($start,$end,$type=1)
    {
        return $this->where (['type'=>$type,'is_complete'=>1])
            ->whereBetweenTime ('create_date',$start,$end)
            ->whereNull('delete_date')
            ->field('order_company_id,certificate_code,performance')
            ->select ()->toArray ();
    }

    /**
     * 删除退单操作
     * @param $id
     * @param $cer
     * @return bool
     */
    public function delCerIdDataModel($id,$cer): bool
    {
        return $this->where (['order_company_id'=>$id,'certificate_code'=>$cer])->useSoftDelete('delete_date',date ('Y-m-d H:i:s'))->delete ();
    }

    /**
     * 查询所有完成的退单
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCharDataModel()
    {
        return $this->where (['type'=>1,'is_complete'=>1])
            ->whereNull('delete_date')
            ->field('id,certificate_code,performance,create_date,reason,order_company_id')
            ->select ()->toArray ();
    }

    /**
     * 查询订单是否有退单操作
     *
     */
    public function getOrderIdCerIsChargeBackModel($id,$cer): ?array
    {
        $data = $this->where (['type'=>1,'is_complete'=>1,'order_company_id'=>$id,'certificate_code'=>$cer])
            ->whereNull('delete_date')
            ->findOrEmpty ();

        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }

    /**
     * 修改退单业绩
     * @param $id
     * @param $cer
     * @param $data
     * @return \app\model\OrderDealChargeback
     */
    public function editDataModel($id,$cer,$data)
    {
        return $this->where (['order_company_id'=>$id,'certificate_code'=>$cer])->update ($data);
    }


    /**
     * 获取退单业绩
     */
    public function getOrderBackPerformance($cid,$cer,$startDate,$endDate)
    {
        return $this->where (['certificate_code'=>$cer,'order_company_id'=>$cid,'type'=>1])
            ->whereBetweenTime ('create_date',$startDate,$endDate)
            ->whereNull('delete_date')
            ->value ('performance');
    }

}